package com.itliang.web.controller.hardware;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itliang.annotation.Log;
import com.itliang.core.model.dto.equipment.EquipmentDataQueryRequest;
import com.itliang.core.model.entity.equipment.EquipmentData;
import com.itliang.core.result.BaseResponse;
import com.itliang.core.result.CodeEnum;
import com.itliang.core.result.ResultUtils;
import com.itliang.enums.BusinessType;
import com.itliang.exception.BusinessException;
import com.itliang.service.hardware.EquipmentDataService;
import io.swagger.annotations.Api;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.itliang.enums.DataType.*;

/**
 * @author : liangzhuoyuan
 */
@Api(tags = "设备数据管理接口")
@RestController
@RequestMapping("/admin/hardware/data")
public class EquipmentDataController {

    @Resource
    private EquipmentDataService equipmentDataService;

    // region 删改查

    /**
     * 删除设备数据
     *
     * @param id
     * @return
     */
    @Log(title = "设备数据管理", businessType = BusinessType.DELETE)
    @GetMapping("/delete")
    public BaseResponse<Boolean> deleteEquipmentData(int id) {
        if (id <= 0) {
            throw new BusinessException(CodeEnum.PARAMS_ERROR);
        }
        boolean b = equipmentDataService.removeById(id);
        return ResultUtils.success(b);
    }

    /**
     * 批量删除设备数据
     *
     * @param idList id集合
     * @return
     */
    @Log(title = "设备数据管理", businessType = BusinessType.BATCHDELETE)
    @PostMapping("/delete/batch")
    public BaseResponse<Boolean> batchDeleteEquipmentData(@RequestBody List<Integer> idList) {
        boolean b = equipmentDataService.removeBatchByIds(idList);
        return ResultUtils.success(b, "恭喜，批量删除操作成功！");
    }

    /**
     * 更新设备数据
     *
     * @param equipmentData
     * @return
     */
    @Log(title = "设备数据管理", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public BaseResponse<Boolean> updateEquipmentData(@RequestBody EquipmentData equipmentData) {
        if (equipmentData == null || equipmentData.getId() == null) {
            throw new BusinessException(CodeEnum.PARAMS_ERROR);
        }
        boolean result = equipmentDataService.updateById(equipmentData);

        return ResultUtils.success(result);
    }

    /**
     * 根据 id 获取设备数据
     *
     * @param id
     * @return
     */
    @GetMapping("/get")
    public BaseResponse<EquipmentData> getEquipmentDataById(int id) {
        if (id <= 0) {
            throw new BusinessException(CodeEnum.PARAMS_ERROR);
        }
        EquipmentData equipmentData = equipmentDataService.getById(id);

        return ResultUtils.success(equipmentData);
    }

    /**
     * 分页获取设备数据列表
     *
     * @param equipmentDataQueryRequest 请求参数
     * @return 设备数据
     */
    @PostMapping("/list/page")
    public BaseResponse<Page<EquipmentData>> listEquipmentDataByPage(@RequestBody EquipmentDataQueryRequest equipmentDataQueryRequest) {

        if (equipmentDataQueryRequest == null) {
            throw new BusinessException(CodeEnum.PARAMS_ERROR);
        }
        // 判断请求数据分类是否正确
        int type = equipmentDataQueryRequest.getDataType();
        if (type != TEMPERATURE.getDataType()
                && type != HUMIDITY.getDataType()
                && type != LIGHT.getDataType()
                && type != CARBON_DIOXIDE.getDataType()) {
            throw new BusinessException(CodeEnum.PARAMS_ERROR);
        }
        // 分页参数
        long current = equipmentDataQueryRequest.getCurrent();
        long size = equipmentDataQueryRequest.getPageSize();
        Page<EquipmentData> dataByPage = equipmentDataService.listEquipmentDataByPage(new Page<>(current, size), equipmentDataQueryRequest);

        return ResultUtils.success(dataByPage);
    }
    // endregion

    /**
     * 获取折线图数据
     *
     * @param dataType 数据类型
     * @return
     */
    @Log(title = "设备数据管理", businessType = BusinessType.FORM)
    @GetMapping("/form")
    public BaseResponse<List> echartsData(Integer dataType, String beginTime, String endTime) {
        QueryWrapper<EquipmentData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("data_type", dataType)
                .ge(StringUtils.hasText(beginTime), "collection_time", beginTime)
                .le(StringUtils.hasText(endTime), "collection_time", endTime);
        List<EquipmentData> list = equipmentDataService.list(queryWrapper);
        ArrayList<Object> resultList = new ArrayList<>();
        for (EquipmentData data : list) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("date", data.getCollectionTime());
            map.put("value", data.getData());
            resultList.add(map);
        }

        return ResultUtils.success(resultList);
    }


}
